 
math.randomseed(1)
-- 平均速度
local vList = 
    {
        800, 800 - 15, 800 - 19, 800 - 8
    }
local split = 10
local range = 30

function genVelMatrix(vList, split)
	local matrix = {}
	for i = 1,#vList do
		matrix[i] = {}
		local v = vList[i]
		for j = 1,split do
			matrix[i][j] = v 
		end
	end
	return matrix
end
--[[

{	 0    0，82 ，
  { 759,759,800,800,759,800,841,800,759,800,800,841,759,841,718,800,841,841,882,800 },
  { 784,785,785,785,783,785,782,787,784,786,787,787,785,785,785,787,785,785,782,786 },
  { 836,789,742,742,789,930,742,883,883,742,789,648,789,789,836,742,836,742,789,742 },
  { 795,800,805,784,800,796,804,788,792,788,804,800,800,800,792,800,800,796,792,784 },
}


]]
function changeSpeedRandom(matrix, split, range )
 	for i = 1,#matrix do
		local list = matrix[i]
		local much = math.random(0,range)
		for j = 1,split do
			local a,b = math.random(1,split),math.random(1,split)
			list[a] = list[a] + much
			list[b] = list[b] - much
		end
	end
end 

function printTable( matrix )
	print("{")
	for i = 1,#matrix do
		local list = matrix[i]
		local str = table.concat (list ,",")
	print("  { ".. str .." },")
	end
	print("}")
end

function calAverageSpeed( matrix )
	for i = 1,#matrix do
		local list = matrix[i]
		local total = 0
		for j = 1,#list do
			total = list[j] + total
		end
		print("index".. i .." :",total/#list)
	end
end
function calAccMatrix( matrix)
	local am = {}
	for i = 1,#matrix do
		local list = matrix[i]
		local split = #list 
		am[i] = {0}

		local v = list[1]
		for j = 2,split do
			v_ = list[j]
			a = (v_ - v) * 2
			v = v + a
			am[i][j] = a
		end
	end
	return am
end
function calAccMatrix2( matrix)
	local am = {}
	for i = 1,#matrix do
		local list = matrix[i]
		local split = #list 
		am[i] = {0}

		local v = list[1]
		for j = 2,split do
			v_ = list[j]
			a = (v_ - v) 
			v = v + a
			table.insert(am[i],a)
			table.insert(am[i],a)
		end
	end
	return am
end

local m = genVelMatrix(vList, split, range)
changeSpeedRandom(m, split, range )
printTable(m)

calAverageSpeed( m )
local am = calAccMatrix2(m)
printTable(am)

  
